package com.axjy.web;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.axjy.pojo.User;
import com.axjy.service.UserService;
import com.axjy.util.Pager;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by asus on 2018/1/27.
 */
@Controller
@RequestMapping("/user")
public class UserHandler {

    @Resource
    private UserService userService;

    @GetMapping("/list")
    public String list(@RequestParam(value="pager.offset",defaultValue = "0") Integer offset,Model model){
        Pager pager=userService.searchPager(offset);
        model.addAttribute(pager);
        return "user/list";
    }

    @GetMapping("/add")
    public String add(@ModelAttribute("user") User user){
        return "user/add";
    }

    @PostMapping("/add")
    public String add(@Validated User user, BindingResult bindingResult, HttpSession session){
       if(bindingResult.hasErrors()){
           return "user/add";
       }
        User loginUser= (User) session.getAttribute("loginUser");
       user.setCreationDate(LocalDateTime.now());
       user.setCreatedBy(loginUser.getId());
       try {
           userService.add(user);
           return "redirect:/user/list";
       }catch(RuntimeException ex){
            return "user/add";
       }
    }


    @GetMapping("/update")
    public String update(Integer id,Model model){
        User user=userService.get(id);
        model.addAttribute("user",user);
        return "user/update";
    }

    @ResponseBody
    @GetMapping("/detail")
    public User detail(Integer id){
        User user=userService.get(id);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = user.getBirthday().atStartOfDay(zoneId);
        Date date = Date.from(zdt.toInstant());
        user.setBornDate(date);
        return user;
    }

    @GetMapping(value="/view")
    @ResponseBody
    public String view(Integer id){
        User user=userService.get(id);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = user.getBirthday().atStartOfDay(zoneId);
        Date date = Date.from(zdt.toInstant());
        user.setBornDate(date);
        SerializerFeature feature=SerializerFeature.WriteDateUseDateFormat;
        String json=JSON.toJSONString(user,feature);
        return json;
    }

    @PostMapping("/update")
    public String update(User user,HttpSession session){
        User loginUser= (User) session.getAttribute("loginUser");
        user.setModifyBy(loginUser.getId());
        user.setModifyDate(LocalDateTime.now());
        boolean flag=userService.update(user);
        if(flag) {
            return "redirect:/user/list";
        }else{
            return "user/update";
        }
    }

    @GetMapping("/{id}/del")
    public String del(@PathVariable Integer id){
        userService.del(id);
        return "redirect:/user/list";
    }

    @GetMapping("/exists")
    @ResponseBody
    public String  exists(String userCode) throws IOException {
       boolean flag= userService.exists(userCode);
       Map<String,Boolean> map=new HashMap<String,Boolean>();
       map.put("flag",flag);
       String json= JSON.toJSONString(map);
       return json;
    }


}
